import functools
from flask import session, current_app, g
from ihome.models import User



def login_required(f):
    """登录验证装饰器"""
    @functools.wraps(f)
    def wrapper(*args,**kwargs):

        user_id = session.get('user_id')
        user = None

        if user_id:
            try:
                user = User.query.filter_by(id=user_id).first()
            except Exception as e:
                current_app.logger.error(e)

        g.user = user
        return f(*args,**kwargs)

    return wrapper


from werkzeug.routing import BaseConverter

class RegexConverter(BaseConverter):
    """自定义静态文件路由转换器"""
    def __init__(self,map,*args):
        super(RegexConverter, self).__init__(map)
        self.regex=args[0]
